package com.lagou.dao;

import com.lagou.bean.Student;
import com.lagou.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class StudentDaoImp implements StudentDao{
    QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

    /**
     * 自定义抽象方法实现学生添加操作
     * @param student
     * @return
     */
    @Override
    public int addStudent(Student student) {

        String sql = "insert into t_student values(?,?,?,?,?,?)";
        Object[] params = {student.getSid(),student.getSname(),student.getSex(),student.getBirthday(),student.getEmail(),student.getContent()};
        int update = 0;
        try {
            update = qr.update(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }

    /**
     * 自定义抽象方法实现学生修改操作
     * @param student
     * @param id
     * @return
     */
    @Override
    public int updateStudent(Student student,String id) {

        String sql = "update  t_student set sid=?,sname=?,sex=?,birthday=?,email=?,content=? where sid = ?";
        Object[] params = {student.getSid(),student.getSname(),student.getSex(),student.getBirthday(),student.getEmail(),student.getContent(),id};
        int update = 0;
        try {
            update = qr.update(sql, params);
            System.out.println(update);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;

    }

    /**
     * 自定义抽象方法实现学生按学号查询操作
     * @param id
     * @return
     */
    @Override
    public int delStudent(String id) {

        String sql = "delete from t_student where sid = ?";
        System.out.println(id);
        int update = 0;
        try {
            update = qr.update(sql, id);
            System.out.println("戹");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }

    /**
     * 自定工抽象方法实现按学号查找学生操作
     * @param id
     * @return
     */
    @Override
    public Student selectStudent(String id) {
        System.out.println(id);
        String sql = "select * from t_student where sid = ?";
        Student student = null;
        try {
            student = qr.query(sql, new BeanHandler<>(Student.class), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return student;

    }
}
